package eteam.aps.systemservice.service;

import cn.hutool.core.convert.Convert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.systeminterface.model.dto.DictionaryDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.DictionaryEntity;
import eteam.aps.systeminterface.model.entity.DictypeEntity;
import eteam.aps.systeminterface.model.reqparam.DicIdParam;
import eteam.aps.systeminterface.model.reqparam.GetBatchDicParam;
import eteam.aps.systeminterface.model.reqparam.GetDicParam;
import eteam.aps.systeminterface.service.IDicController;
import eteam.aps.systemservice.bll.DicBll;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 字典服务
 */
@Api(tags = "字典服务控制器")
@RestController
//@RequestMapping(value = "/api/dic")
public class DicController implements IDicController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private DicBll dicBll;

    /**
     * 查询数据字典列表
     *
     * @return
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询数据字典列表")
    @Override
    @GetMapping(value = "/api/dic/getdiclist")
    public R<List<DictionaryEntity>> getDicList() throws Exception, AuthException {
        return R.success(dicBll.getDicList());
    }

    /**
     * 查询数据字典分类列表
     *
     * @return
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查询数据字典分类列表")
    @Override
    @GetMapping(value = "/api/dic/getdictypelist")
    public R<List<DictypeEntity>> getDicTypeList() throws Exception, AuthException {
        return R.success(dicBll.getDicTypeList());
    }

    /**
     * 新增数据字典
     *
     * @param dicDto: 数据字典
     * @return
     */
    @ApiOperationSupport(order = 3, ignoreParameters = {"dicDto.fdicid"})
    @ApiOperation(value = "新增数据字典")
    @Override
    @PostMapping(value = "/api/dic/createdic")
    public R createDic(@Validated(Insert.class) @RequestBody DictionaryDto dicDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        dicBll.createDicValue(tokenDto, dicDto);
        return R.success();
    }

    /**
     * 更新数据字典
     *
     * @param dicDto: 数据字典信息
     * @return
     */
    @ApiOperationSupport(order = 4, ignoreParameters = {})
    @ApiOperation(value = "更新数据字典")
    @Override
    @PostMapping(value = "/api/dic/updatedic")
    public R updateDic(@Validated(Update.class) @RequestBody DictionaryDto dicDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        dicBll.updateDicValue(tokenDto, dicDto);
        return R.success();
    }

    /**
     * 删除数据字典
     *
     * @param dicParam: 数据字典Id参数
     * @return
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除数据字典")
    @Override
    @PostMapping(value = "/api/dic/deletedic")
    public R deleteDic(@Validated @RequestBody DicIdParam dicParam) throws Exception, AuthException {
        dicBll.deleteDicValue(dicParam.getDicid());
        return R.success();
    }

    /**
     * 根据分类编码获取字典列表
     *
     * @param getDicParam
     * @return
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "根据分类编码获取字典列表")
    @Override
    @GetMapping(value = "/api/dic/getdicbytype")
    public R<List<DictionaryEntity>> getDicByType(@Validated GetDicParam getDicParam) throws Exception, AuthException {
        return R.success(dicBll.getDicByType(getDicParam.getDictypecode()));
    }

    /**
     * 根据分类编码集合获取字典列表
     *
     * @param getBatchDicParam
     * @return
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "根据分类编码获取字典列表")
    @Override
    @GetMapping(value = "/api/dic/getdicbytypes")
    public R<List<DictionaryEntity>> getDicByTypes(@Validated GetBatchDicParam getBatchDicParam) throws Exception, AuthException {
        return R.success(dicBll.getDicByType(getBatchDicParam.getDictypecodes()));
    }

    /**
     * 获取物料参数所需字典分类列表
     *
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "获取物料参数所需字典分类列表")
    @Override
    @GetMapping(value = "/api/dic/getmtrparamdictypelist")
    public R<List<DictypeEntity>> getMtrParamDicTypeList() throws Exception, AuthException {
        return R.success(dicBll.getMtrParamDicTypeList());
    }
}
